package com.bop.dehong.more;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.PreferencesUtils;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.GetUserInfoModel;
import com.bop.dehong.huodong.HuodongAct;
import com.bop.dehong.info.UserInfo;
import com.bop.dehong.login.LoginActivity;
import com.bop.dehong.main.MainActivity;
import com.bop.dehong.util.ImageLoaderUtils;
import com.bop.dehong.util.SharePopUtil;
import com.bop.dehong.widget.CircleImageView;

/**
 * 更多
 * @author TangWenhan/YeFengyun
 */
public class MoreActivity extends BaseActivity{
	
	private CircleImageView mHeadView;
	private	TextView mNameTv;
	private GetUserInfoModel mGetInfoModel;
	private final int  RESULT_FOR_LOGIN = 101;

	@Override
	protected void initTop() {
		View mView = getView(R.id.top);
		TopManager manager = new TopManager(mView);
		manager.setTitle(R.string.more);
		manager.setLeftImageVisibility(View.VISIBLE);
		registerOnClickListener(getView(R.id.logout_btn), getView(R.id.contactus_tv), getView(R.id.go2activitycenter),
				getView(R.id.invitefriends_tv), getView(R.id.aboutus_tv), getView(R.id.feedback_tv));
		
	}

	@Override
	protected void initUI() {
		// TODO Auto-generated method stub
		mHeadView = getView(R.id.imageView2);
		mNameTv = getView(R.id.name);
	}

	@Override
	protected void initData() {
		if (DeHongApp.mUserInfo != null ) {
			if (!TextUtils.isEmpty(DeHongApp.mUserInfo.head)) {
				ImageLoaderUtils.getInstance(this).setImageNetResource(mHeadView, DeHongApp.mUserInfo.head);
			}
			if ("2".equals(DeHongApp.mUserInfo.certification)) {
				mNameTv.setText(DeHongApp.mUserInfo.name);
			}else{
				mNameTv.setText(DeHongApp.mUserInfo.username);
			}
		}else {
			reqInfo();
		}
		
	}

	private void reqInfo(){
		if (DeHongApp.mLoginEntity != null ) {
			if (mGetInfoModel == null) {
				mGetInfoModel = new GetUserInfoModel(this, getCallback);
			}
			showProgressDialog();
			mGetInfoModel.doNet(DeHongApp.mLoginEntity.uid);
		}else {
			Intent intent = new Intent(this, LoginActivity.class);
			intent.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent, RESULT_FOR_LOGIN);
		}
		
	}
	RequestCallBack<UserInfo> getCallback = new RequestCallBack<UserInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(UserInfo info) {
			super.onSuccess(info);
			DeHongApp.mUserInfo = info;
			initData();
		}
		
	};
	
	
	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.activity_personal_more;
	}
	/**
	 * 活动中心
	 * @param v
	 */
	public void goActivityCenter(){
		openActivity(HuodongAct.class);
//		openActivity(HuodongActivity.class);
	}
	/**
	 * 邀请好友
	 * @param v
	 */
	public void goInviteFriends(View v){
		SharePopUtil sharePopUtil = new SharePopUtil(this, v);
		sharePopUtil.setShareParams(Config.SHARE_TITLE,
				 Config.SHARE_IMG,
				Config.SHARE_CONTENT, Config.SHARE_URL, SharePopUtil.shareNetImg);
	}
	/**
	 * 关于德鸿
	 * @param v
	 */
	public void goAboutDeHong(){
		openActivity(AboutDeHongActivity.class);
	}
	/**
	 * 意见反馈
	 * @param v
	 */
	public void goFeedback(){
		openActivity(FeedbackActivity.class);
	}
	/**
	 * 联系我们
	 * @param v
	 */
	public void goConnectionUS(){
		final Dialog connectDialog = new Dialog(this, R.style.MyDialogStyle);
		View view = View.inflate(this, R.layout.dialog_connect_us, null);
		connectDialog.setContentView(view);
		getView(view, R.id.cancel).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				connectDialog.dismiss();
			}
		});
		getView(view, R.id.call).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				connectDialog.dismiss();
				Intent intent = new Intent(Intent.ACTION_CALL);  
				intent.setData(Uri.parse("tel:"+getResources().getString(R.string.contact_phone)));  
				startActivity(intent);  
			}
		});
		connectDialog.show();
		 
	}
	Dialog mExitDialog;
	private void showExitDialog(){
		if (mExitDialog == null) {
			mExitDialog = new Dialog(this, R.style.MyDialogStyle);
			View view = View.inflate(this, R.layout.dialog_forget_pwd, null);
			TextView contentView = getView(view, R.id.textView2);
			contentView.setText("确定要安全退出吗？");
			Button mOKBtn = getView(view, R.id.re_login);
			mOKBtn.setText("确定");
			mExitDialog.setContentView(view);
			mExitDialog.setCanceledOnTouchOutside(true);
			registerOnClickListener(mOKBtn,getView(view, R.id.re_cancel));
		}
		if (!mExitDialog.isShowing()) {
			mExitDialog.show();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mExitDialog != null) {
			mExitDialog.dismiss();
			mExitDialog = null;
		}
	}
	
	
	public void goLoginout() {
		showExitDialog();
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		else if (requestCode == RESULT_FOR_LOGIN) {
			reqInfo();
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.re_login:
			if (mExitDialog != null) {
				mExitDialog.dismiss();
			}
			DeHongApp.mLoginEntity = null;
			DeHongApp.mUserInfo = null;
			PreferencesUtils.putString(this, Config.UID, "");
			PreferencesUtils.putString(this, Config.Platform, "");
			PreferencesUtils.putString(this, Config.NickName, "");
//			PreferencesUtils.putString(this, Config.USERNAME, "");
			PreferencesUtils.putString(this, Config.PASSWORD, "");
			String name = PreferencesUtils.getString(this, Config.USERNAME);
			PreferencesUtils.putString(this, Config.GESTURE_PWD + name, "");
			ToastUtils.show(this, "退出成功");
			Intent intent = new Intent(this,MainActivity.class);
			intent.putExtra("page", 0);
			startActivity(intent);
//			setResult(RESULT_OK);
			finishCurrentActivity();
			break;
		case R.id.re_cancel:
			if (mExitDialog != null) {
				mExitDialog.dismiss();
			}
			break;
		case R.id.logout_btn:
			goLoginout();
			break;
		case R.id.contactus_tv:
			goConnectionUS();
			break;
		case R.id.go2activitycenter:
			goActivityCenter();
			break;
		case R.id.invitefriends_tv:
			goInviteFriends(v);
			break;
		case R.id.aboutus_tv:
			goAboutDeHong();
			break;
		case R.id.feedback_tv:
			goFeedback();
			break;
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}	
}
